package com.xinpu.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xinpu.entity.TSalesRole;
import com.xinpu.service.ITSalesRoleService;
import com.xinpu.service.SalesRoleService;
import com.xinpu.vo.ResponseMessage;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * SalesRoleController
 *
 * @author ayuan
 * @date 2021/5/8
 */
@RestController
@RequestMapping("/salesRole")
public class SalesRoleController {

    @Resource
    private ITSalesRoleService itSalesRoleService;

    @Resource
    private SalesRoleService salesRoleService;


    /**
     * 全部角色
     *
     * @return
     */
    @GetMapping("/allList")
    public ResponseMessage allList() {
        return ResponseMessage.ok(itSalesRoleService.list());
    }


    /**
     * 顶级角色列表
     *
     * @return
     */
    @GetMapping("/upList")
    public ResponseMessage upList() {
        return ResponseMessage.ok(itSalesRoleService.list(Wrappers.<TSalesRole>lambdaQuery().eq(TSalesRole::getPid, 0)));
    }

    /**
     * 角色列表
     *
     * @return
     */
    @GetMapping("/list")
    public ResponseMessage list() {
        return ResponseMessage.ok(salesRoleService.findList());
    }

    /**
     * 添加或编辑销售角色
     *
     * @param map
     * @return
     */
    @PostMapping("/createOrEdit")
    public ResponseMessage createOrEdit(@RequestBody Map<String, String> map) {
        TSalesRole tSalesRole = new TSalesRole();
        String id = map.get("id");
        if (StringUtils.isNotBlank(id)) {
            tSalesRole.setId(Integer.parseInt(id));
        }
        String pid = map.get("pid");
        if (StringUtils.isNotBlank(pid)) {
            tSalesRole.setPid(Integer.parseInt(pid));
        }
        tSalesRole.setName(map.get("name"));
        itSalesRoleService.saveOrUpdate(tSalesRole);
        return ResponseMessage.ok();
    }

    /**
     * 删除角色
     *
     * @param map
     * @return
     */
    @PostMapping("/delete")
    public ResponseMessage delete(@RequestBody Map<String, String> map) {
        itSalesRoleService.removeById(map.get("id"));
        return ResponseMessage.ok();
    }

}
